package com.yyds.blackmagic.controller;

import com.yyds.blackmagic.utils.QiNiuUtils;
import com.yyds.blackmagic.vo.ActionResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

/**
 * @Author ccc
 * @Date 2021/12/18
 * @Descripton
 */
@RestController
@RequestMapping("/api")
public class ImgUploadController {
    @PostMapping("/admin/content/books/covers")
    public ActionResult upload(MultipartFile file){
        String preName = UUID.randomUUID ().toString ();
        preName = preName.replace ("-","");
        String subName = file.getOriginalFilename ().substring (file.getOriginalFilename ().indexOf ("."));
        String fileName = preName+subName;
        try {
            String filePath = QiNiuUtils.updateFile (file, fileName);
            return new ActionResult (200,"上传成功",filePath);
        } catch (Exception e) {
            e.printStackTrace ();
        }
        return new ActionResult (500,"上传失败",null);
    }
}
